
/**
 * LXLRobot机器人扩展
 * 点歌扩展
 * 
 * 发送    点歌 歌名
 */

let config = YoyoClass.JsonConfig(".\\music\\config.json", data.toJson({
    group: ['569211842']
}));

PluginsClass.listen("onGroupMessage", (group, user, msg) => {
    let Limtgroup = config.get("group");
    let isGroup = Limtgroup.filter(groupid => groupid == group.id);
    if (isGroup.length >= 1) {//对指定群进行操作
        if (/^点歌\s?(.+)$/.test(msg.text)) {
            let pezs = /^点歌\s?(.+)$/.exec(msg.text);
            if(pezs[1]=='八连杀'){
                let musicUrl = 'http://yzq.xclnet.cn/MCBE/res/bls.mp3';
                let musicas = '苏三';
                let musicName = '八连杀';
                let musiclink = 'http://baidu.com';
                let musicpic = 'http://yzq.xclnet.cn/MCBE/res//ss.png';
                let msgs = `<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[点歌] ${musicName}" sourceMsgId="0" url="${musiclink}" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="${musicpic}" src="${musicUrl}" /><title>${musicName}</title><summary>${musicas}</summary></item><source name="QQ" icon="" action="app" i_actionData="tencent1101244924://" appid="1101244924" /></msg>`;
                RobotApiClass.sendGroupMsg(group.id, RobotApiClass.xml(msgs));
                return;
            }
            network.httpGet(`http://api.guaqb.cn/music/music/?input=${pezs[1]}&filter=name&type=163`, (status, result) => {
                if (status != 200) {
                    RobotApiClass.sendGroupMsg(group.id, `歌曲接口出错!`);
                    return;
                }
                let musicList = JSON.parse(result);
                if (musicList.code != 200) {
                    RobotApiClass.sendGroupMsg(group.id, musicList.error);
                    return;
                }
                let musicUrl = musicList.data[0]['music'];
                let musicas = musicList.data[0]['author'];
                let musicName = musicList.data[0]['name'];
                let musiclink = musicList.data[0]['link'];
                let musicpic = musicList.data[0]['pic'];
                let msgs = `<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[点歌] ${musicName}" sourceMsgId="0" url="${musiclink}" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="${musicpic}" src="${musicUrl}" /><title>${musicName}</title><summary>${musicas}</summary></item><source name="QQ" icon="" action="app" i_actionData="tencent1101244924://" appid="1101244924" /></msg>`;
                RobotApiClass.sendGroupMsg(group.id, RobotApiClass.xml(msgs));

            });
        }
    }
});


PluginsClass.listen("onChumMessage", musicS);



function musicS(user, msg) {
    if (/^点歌\s?(.+)$/.test(msg.text)) {
        let pezs = /^点歌\s?(.+)$/.exec(msg.text);
        network.httpGet(`https://api.guaqb.cn/music/music/?input=${pezs[1]}&filter=name&type=163`, (status, result) => {
            if (status != 200) {
                RobotApiClass.sendChumMsg(user.id, `歌曲接口出错!`);
                return;
            }
            let musicList = JSON.parse(result);
            if (musicList.code != 200) {
                RobotApiClass.sendChumMsg(user.id, musicList.error);

                return;
            }
            let musicUrl = musicList.data[0]['music'];
            let musicas = musicList.data[0]['author'];
            let musicName = musicList.data[0]['name'];
            let musiclink = musicList.data[0]['link'];
            let musicpic = musicList.data[0]['pic'];
            let msgs = `<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[点歌] ${musicName}" sourceMsgId="0" url="${musiclink}" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="${musicpic}" src="${musicUrl}" /><title>${musicName}</title><summary>${musicas}</summary></item><source name="QQ" icon="" action="app" i_actionData="tencent1101244924://" appid="1101244924" /></msg>`;
            RobotApiClass.sendChumMsg(user.id, RobotApiClass.xml(msgs));
        });
    }
}